package cn.com.zh.app.userManager.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.zh.app.common.util.ResultVO;
import cn.com.zh.app.userManager.dto.UserInfoDTO;
import cn.com.zh.app.userManager.form.UserInfoForm;
import cn.com.zh.app.userManager.service.UserInfoService;

//@Controller
@RestController
public class UserInfoController {

	private Logger logger = LoggerFactory.getLogger(UserInfoController.class);
	
	@Resource
	private UserInfoService userInfoService;

	@RequestMapping("/account/logIn")
	public ResultVO<Object> loginIn(HttpServletRequest request, HttpServletResponse response,
			@RequestBody UserInfoForm form) {
		ResultVO<Object> vo = new ResultVO<Object>();
		try {
			if (StringUtils.isBlank(form.getLoginId()) || StringUtils.isBlank(form.getPassWord())) {
				vo.setCode("999001");
				vo.setMessage("传入参数错误");
			} else {
				UserInfoDTO dto = new UserInfoDTO();
				dto.setLoginId(Integer.parseInt(form.getLoginId()));
				dto.setPassword(form.getPassWord());
				UserInfoDTO userInfoDTO = userInfoService.getUserInfo(dto);
				if (userInfoDTO == null) {
					vo.setCode("100000");
					vo.setMessage("登陆失败,账户/密码不正确");
				} else {
					vo.setCode("000000");
					vo.setMessage("登陆成功");
				}
			}
		} catch (Exception e) {
			logger.error(e.toString());
			vo.setCode("999999");
			vo.setMessage("系统异常");
		}
		return vo;
	}

	@RequestMapping("/account/registorLogin")
	public ResultVO<Object> registorLoginId(HttpServletRequest request, HttpServletResponse response,
			@RequestBody UserInfoForm form) {
		ResultVO<Object> vo = new ResultVO<Object>();
		try {
			UserInfoDTO dto = new UserInfoDTO();
			dto.setLoginId(Integer.parseInt(form.getLoginId()));
			dto.setPassword(form.getPassWord());
			UserInfoDTO userInfoDTO = userInfoService.registorUserInfo(dto);
			if (userInfoDTO == null) {
				vo.setCode("100000");
				vo.setMessage("注册失败,请稍候重试!");
			} else {
				vo.setCode("000000");
				vo.setMessage("注册成功!");
				vo.setBody(userInfoDTO);
			}

		} catch (Exception e) {
			logger.error(e.toString());
			vo.setCode("300000");
			vo.setMessage("注册失败,请稍候重试!");
		}

		return vo;
	}

	@RequestMapping("/account/loginOut")
	public ResultVO<Object> loginOut(HttpServletRequest request, HttpServletResponse response,
			@RequestBody UserInfoForm form) {
		ResultVO<Object> vo = new ResultVO<Object>();

		return vo;
	}
}
